package com.xxmw.transaction.task;

import com.alibaba.fastjson.JSONObject;
import com.xxmw.transaction.common.utils.http.HttpClientUtil;
import com.xxmw.transaction.controller.server.TransactionWebsocketServer;
import com.xxmw.transaction.enumeration.TopicEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@Component
public class OkexWebSocketTask {

    @Scheduled(cron = "0/1 * *  * * ? ")
    public void pushMinuteKline() {
        sendWebSocketMsg(KlineType.ONE_MINUTE);
    }

    @Scheduled(cron = "0/10 * *  * * ? ")
    public void pushFiveMinuteKline() {
        sendWebSocketMsg(KlineType.FIVE_MINUTES);
    }

    @Scheduled(cron = "0/12 * *  * * ? ")
    public void pushFifteenMinuteKline() {
        sendWebSocketMsg(KlineType.FIVTEEN_MINUTES);
    }

    @Scheduled(cron = "0/13 * *  * * ? ")
    public void pushThirtyMinuteKline() {
        sendWebSocketMsg(KlineType.THIRTY_MINUTES);
    }

    @Scheduled(cron = "0/14 * *  * * ? ")
    public void pushOneHourKline() {
        sendWebSocketMsg(KlineType.ONE_HOUR);
    }

    @Scheduled(cron = "0/15 * *  * * ? ")
    public void pushFourHoursKline() {
        sendWebSocketMsg(KlineType.FOUR_HOURS);
    }

    @Scheduled(cron = "0/16 * *  * * ? ")
    public void pushOneDayKline() {
        sendWebSocketMsg(KlineType.ONE_DAY);
    }

    /**
     * 根据传入的K线类型不同发送不同的k线数据
     * @param klineType  K线类型
     */
    private void sendWebSocketMsg(KlineType klineType) {
        JSONObject object = new JSONObject();
        object.put("klineType",klineType.type());
        AtomicInteger count = new AtomicInteger();
        ConcurrentHashMap<Long, TransactionWebsocketServer> serverMap = TransactionWebsocketServer.getServer();
        if (!serverMap.isEmpty()) {
            serverMap.forEach((id, server) -> {
                //订阅了发送消息
                if (server.isSubscribed(TopicEnum.OKEX_KLINE.getDesc())) {
                    count.getAndIncrement();
                    if (count.get() == 1) {
                        //获取分时线
                        String res = HttpClientUtil.get("https://www.okex.com/api/futures/v3/instruments/BTC-USD-200403/candles?granularity="+klineType.param());
                        object.put("data",JSONObject.parseArray(res));
                    }
                    server.senObject(object);
                }
            });
        }
    }

    private enum KlineType {
        ONE_MINUTE(0, "60"),
        FIVE_MINUTES(1, "300"),
        FIVTEEN_MINUTES(2, "900"),
        THIRTY_MINUTES(3, "1800"),
        ONE_HOUR(4, "3600"),
        FOUR_HOURS(5, "14400"),
        ONE_DAY(6, "86400");
        private Integer type;

        private String param;

        KlineType (Integer type, String param) {
            this.type = type;
            this.param = param;
        }

        public String param () {
            return this.param;
        }

        public Integer type () {
            return this.type;
        }
    }
}
